﻿using Domain.QualityInspection_Modelling;
using MediatR;
using MES.Server.API.Application.Command.QualityInspection_ModCom;
using MES.Server.API.Dto.QualityInspection_DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReturnValue;

namespace MES.Server.API.Controllers
{
    /// <summary>
    /// 质检Api
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class QualityInspectionController : ControllerBase
    {
        private readonly IMediator _mediator;

        public QualityInspectionController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 创建质检记录
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<ApiResult<QualityInspection>>> Create([FromBody] CreateQualityInspectionCommand command)
        {
            return Ok(await _mediator.Send(command));
        }

        /// <summary>
        /// 查询质检记录（支持分页和条件筛选）
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<ApiPaging<QualityInspection>>> Get(
            [FromQuery] long? id,
            [FromQuery] long? productionOrderId,
            [FromQuery] long? materialGroupId,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 20)
        {
            var command = new GetQualityInspectionsCommand
            {
                Id = id,
                ProductionOrderId = productionOrderId,
                MaterialGroupId = materialGroupId,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            return Ok(await _mediator.Send(command));
        }

        /// <summary>
        /// 更新质检记录
        /// </summary>
        [HttpPut]
        public async Task<ActionResult<ApiResult<QualityInspection>>> Update([FromBody] UpdateQualityInspectionCommand command)
        {
            return Ok(await _mediator.Send(command));
        }

        /// <summary>
        /// 删除质检记录
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult<object>>> Delete(long id)
        {
            return Ok(await _mediator.Send(new DeleteQualityInspectionCommand { Id = id }));
        }


        /// <summary>
        /// 获取质检记录列表（分页）
        /// </summary>
        /// <param name="id">质检记录ID（可选）</param>
        /// <param name="productionOrderId">生产工单ID（可选）</param>
        /// <param name="materialGroupId">物料群组ID（可选）</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>分页质检记录DTO</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<QualityInspectionDTO>>> QualityInspectionQuery([FromQuery]QualityInspectionQueryCommand request)
        {

            return  await _mediator.Send(request);
        }

        /// <summary>
        /// 创建质检记录
        /// </summary>
        /// <param name="dto">创建质检记录DTO</param>
        /// <returns>创建结果及质检记录DTO</returns>
        [HttpPost]
        public async Task<ApiResult<int>> QualityInspectionCreate(QualityInspectionCreateCommands request)
        {
            return (await _mediator.Send(request));
        }

        /// <summary>
        /// 更新质检记录
        /// </summary>
        /// <param name="dto">更新质检记录DTO</param>
        /// <returns>更新结果及质检记录DTO</returns>
        [HttpPut]
        public async Task<ActionResult<ApiResult<QualityInspectionDto>>> UpdateQualityInspection([FromBody] UpdateQualityInspectionDto dto)
        {
            var command = new UpdateQualityInspectionCommands { Dto = dto };
            return Ok(await _mediator.Send(command));
        }

        /// <summary>
        /// 删除质检记录
        /// </summary>
        /// <param name="id">质检记录ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete]
        public async Task<ActionResult<ApiResult<object>>> DeleteQualityInspection(long id)
        {
            var command = new DeleteQualityInspectionCommands { Id = id };
            return Ok(await _mediator.Send(command));
        }

        /// <summary>
        /// 获取生产工单-物料群组二级联动数据
        /// </summary>
        /// <returns>二级联动数据DTO</returns>
        [HttpGet]
        public async Task<ActionResult<ApiResult<ProductionCascaderDto>>> GetCascaderData()
        {
            var command = new GetProductionCascaderCommands();
            return Ok(await _mediator.Send(command));
        }
    }
}
